package com.oepm.web.controller;

import com.oepm.biz.TimeBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.TimePo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/time")
public class TimeController {
    @Autowired
    private TimeBiz timeBiz;

    /**
     * 查询项目立项时间与截止时间
     * @return
     */
    @GetMapping("/select")
    public Result selectAll(){
        List<TimePo> list = timeBiz.selectAll();
        Result result = new Result(Meta.SUCCESS);
        result.setData(list);
        return result;
    }

    /**
     * 跟新
     * @param timePo
     * @return
     */
    @PostMapping("/update")
    public Result updateTime(@RequestBody TimePo timePo){
        int flag = timeBiz.updateTime(timePo);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 插入时间数据
     * @param timePo
     * @return
     */
    @PostMapping("/insert")
    public Result insertTime(@RequestBody TimePo timePo){
        int flag = timeBiz.insertTime(timePo);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result deleteTime(@PathVariable String id){
        int flag = timeBiz.deleteTime(id);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }


    /**
     *
     * @return
     */
    @GetMapping("/getDefence")
    public Result getDefence(HttpServletRequest request){
        //从请求体重获取用户的学院信息
        String token = request.getHeader("Authorization");
        String academy = JWTUtil.getAcademy(token);
        String roleId = JWTUtil.getRoleId(token);
        if(roleId.equals("1")){
            academy = null;
        }
        List<TimePo> timePos = timeBiz.queryAllByAcademy(academy);
        Result result = new Result(Meta.SUCCESS);
        result.setData(timePos);
        return result;
    }
}
